﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.Configuration;

namespace TopXML2SQL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ReadXml_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (this.openFileDialog1.OpenFile() != null)
                {
                    AuthorsDataSet.ReadXml(@openFileDialog1.FileName);
                    dataGridView1.DataSource = AuthorsDataSet;
                    dataGridView1.DataMember = "item_cat";
                }
            }
        }

        private void InsertSQL_Click(object sender, EventArgs e)
        {
            ConnectionStringSettings strconn = ConfigurationManager.ConnectionStrings["DBConnString"];
            SqlConnection sqlconn = new SqlConnection(strconn.ConnectionString);
            sqlconn.Open();
            StringBuilder commandString = new StringBuilder();
            string commandWork = "";
            string commandValues = "";
            for (int i = 0; i < this.dataGridView1.RowCount-1; i++)
            {
                
                for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
                {
                    string ColumsName = this.dataGridView1.Columns[j].Name;
                    string ColumsValue = this.dataGridView1.Rows[i].Cells[j].Value.ToString();
                    commandWork += "[" + ColumsName + "],";
                    if (ColumsName == "name" || ColumsName == "status" || ColumsName == "is_parent")
                    {
                        ColumsValue = "'" + ColumsValue + "'";
                    }
                    commandValues += ColumsValue + ",";
                }

                commandWork = commandWork.Substring(0, commandWork.Length - 1);
                commandValues = commandValues.Substring(0, commandValues.Length - 1);
                commandString.Append("INSERT INTO Category(" + commandWork + ") VALUES(" + commandValues + ")");

                commandWork = "";
                commandValues = "";
            }

            SqlCommand comm = new SqlCommand(commandString.ToString(), sqlconn);
            
            try
            {

                comm.ExecuteNonQuery();
                sqlconn.Close();
                MessageBox.Show("添加完毕!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                sqlconn.Close();
            }
            
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                dataGridView1.RowHeadersWidth - 4,
                e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void DeleteTable_Click(object sender, EventArgs e)
        {
            ConnectionStringSettings strconn = ConfigurationManager.ConnectionStrings["DBConnString"];
            SqlConnection sqlconn = new SqlConnection(strconn.ConnectionString);
            sqlconn.Open();
            SqlCommand comm = new SqlCommand("DELETE FROM Category", sqlconn);
            try
            {
                comm.ExecuteNonQuery();
                sqlconn.Close();
                MessageBox.Show("目录表已清空，可导入新数据!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                sqlconn.Close();
            }

        }
    }
}
